package com.hm.health.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.goodix.libromablecom.sdk.cardiff.CardiffConstants;
import com.hm.health.R;

/**
 * Created by jiaguang on 2018/12/25.
 */

public class ClockView extends View {
    private static final String TAG = "ClockView";
    private Paint mScalePaint;//刻度画笔
    private Paint mTextPaint;//文字画笔
    private Paint mPointerPaint;//指针画笔
    private Context mContext;
    private boolean mIsInTest = true;
    private int mClockViewWidth;
    private int mClockViewHeight;
    private int mClockPanRadius;
    private int mScaleWidth;
    private int mScaleLeft;
    private int mScaleRight;
    private int mScaleBottom;
    private int mScaleTop;
    private int mProcess;//测试进度
    private int mProcessMax;//测试进度最大值
    private int mPressIndex;//压力指数结果
    private int mFatigueIndex;//疲劳指数结果
    private int mPhysiologyAgeIndex;//生理年龄结果

    public ClockView(Context context){
        this(context, null);
    }

    public ClockView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        mContext = context;
        initPaint();
    }
    private void initPaint(){

        mClockPanRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                8,getResources().getDisplayMetrics());
        mScaleWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                3,getResources().getDisplayMetrics());

        mScalePaint = new Paint();
        mScalePaint.setAntiAlias(true);
        mScalePaint.setStyle(Paint.Style.FILL);
        mScalePaint.setColor(Color.parseColor("#ffa500"));

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setStrokeWidth(1.0f);

        mPointerPaint = new Paint();
        mPointerPaint.setAntiAlias(true);
        mPointerPaint.setFilterBitmap(true);
        mPointerPaint.setStyle(Paint.Style.FILL);
        mPointerPaint.setColor(Color.parseColor("#ffa500"));
        mPointerPaint.setStrokeWidth(6.0f);
    }

    public void setProcess(int process){
        mProcess = process;
    }

    public void setProcessMax(int procesMax){
        mProcessMax = procesMax;
    }

    public void showResult(int[] rawDataAndResult){
        mIsInTest = false;
        mPressIndex = rawDataAndResult[CardiffConstants.PFACalculateParamEnum.PRESS.ordinal()];
        mFatigueIndex = rawDataAndResult[CardiffConstants.PFACalculateParamEnum.FATIGUE.ordinal()];
        mPhysiologyAgeIndex = rawDataAndResult[CardiffConstants.PFACalculateParamEnum.AGE.ordinal()];
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas){
        drawScales(canvas);
        if(mIsInTest){
            drawPointer(canvas);
        } else {
            drawResult(canvas);
        }
    }

    private void drawResult(Canvas canvas){
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(50);
        canvas.drawText(mContext.getString(R.string.press_index), mClockViewWidth / 2,
                mClockViewHeight * 3 / 8, mTextPaint);
        mTextPaint.setTextSize(100);
        canvas.drawText(Integer.toString(mPressIndex), mClockViewWidth / 2,
                mClockViewHeight / 2, mTextPaint);
        mTextPaint.setTextSize(50);
        canvas.drawText(mFatigueIndex + "(" + mContext.getString(R.string.fatigue_index) +
                   ")" + ", " + mPhysiologyAgeIndex + "(" + mContext.getString(R.string.age_index) + ")",
                mClockViewWidth / 2, mClockViewHeight * 5 / 8, mTextPaint);
    }

    private void drawPointer(Canvas canvas){
        canvas.drawCircle(mClockViewWidth / 2, mClockViewHeight / 2, 14, mPointerPaint);//绘制表针交点\
        canvas.save();
        canvas.rotate(-360 * mProcess / mProcessMax, mClockViewWidth / 2, mClockViewWidth / 2);
        canvas.drawLine(mClockViewWidth / 2, mClockViewHeight / 2,
                mClockViewWidth / 2 , mClockPanRadius, mPointerPaint);
        canvas.drawLine(mClockViewWidth / 2, mClockViewHeight / 2,
                mClockViewWidth / 2 , mClockViewWidth / 2 + mClockViewWidth / 20, mPointerPaint);
        canvas.restore();
    }

    private void drawScales(Canvas canvas) {
        for (int i = 0; i <= 360; i++) {
            mScaleBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getResources().getDisplayMetrics());
            if (i % 3 == 0) {
                canvas.save();
                canvas.rotate(i, mClockViewWidth / 2, mClockViewHeight / 2);
                canvas.drawRect(mScaleLeft, mScaleTop, mScaleRight, mScaleBottom, mScalePaint);
                canvas.restore();
            }
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mClockViewWidth = w;
        mClockViewHeight = h;
        mScaleLeft = mClockViewWidth / 2 - mScaleWidth / 2;
        mScaleRight = mClockViewWidth / 2 + mScaleWidth / 2;
        mScaleTop = mClockPanRadius;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(onMeasureSpec(widthMeasureSpec), onMeasureSpec(heightMeasureSpec));
    }

    private int onMeasureSpec(int measureSpec) {
        int resultSpec = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                300, getResources().getDisplayMetrics());
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.AT_MOST) {
            resultSpec = Math.min(resultSpec, specSize);
        } else if (specMode == MeasureSpec.EXACTLY) {
            resultSpec = specSize;
        }
        return resultSpec;
    }
}
